package com.zkzx.operation.api.service.order;

import com.zkzx.common.company.model.CompanyDriverVO;
import com.zkzx.common.company.model.CompanyVO;
import com.zkzx.common.freight.constants.OrderUsualEnum;
import com.zkzx.common.freight.model.OrderListVO;
import com.zkzx.common.freight.model.OrderVO;
import com.zkzx.common.freight.param.OrderFilterParam;
import com.zkzx.common.global.Global;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.operation.api.client.company.CompanyClient;
import com.zkzx.operation.api.client.driver.DriverClient;
import com.zkzx.operation.api.client.order.OrderClient;
import com.zkzx.operation.api.client.truck.TruckClient;
import com.zkzx.util.json.JSONResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 * @Description:运单接口
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/12/20:01
 */
@Slf4j
@Service
public class OrderService {

    @Autowired
    private OrderClient orderClient;
    @Autowired
    private DriverClient driverClient;
    @Autowired
    private TruckClient truckClient;
    @Autowired
    private CompanyClient companyClient;

    public PageResult<OrderListVO> find(OrderFilterParam filterParam,RequestFilterParams params, Integer userId)  {

        String filterParamJson = JSONResultUtil.object2JSON(filterParam);
        String paramsJson = JSONResultUtil.object2JSON(params);

        PageResult<OrderListVO> pageResult = orderClient.find(filterParamJson, paramsJson);

        List<OrderListVO> result = pageResult.getResult();

        if (CollectionUtils.isEmpty(result)){
            return new PageResult<>(params.getPage(),params.getPerPage(), Collections.emptyList(), Global.ZERO);
        }

        result.forEach(orderListVO->{

            List<CompanyDriverVO> companyDriverVOs = companyClient.getByDriverIdAndCompanyId(Integer.parseInt(orderListVO.getDriverId()), orderListVO.getCompanyId());
            if (!CollectionUtils.isEmpty(companyDriverVOs)){
                orderListVO.setDriverMobile(companyDriverVOs.get(0).getMobile());
            }
           orderListVO.setTruckVO(truckClient.getById(Integer.parseInt(orderListVO.getTruckId()),orderListVO.getCompanyId(),userId));
           CompanyVO companyVO = companyClient.getById(orderListVO.getCompanyId());
           if (Objects.nonNull(companyVO)){
               orderListVO.setCompanyName(companyVO.getName());
           }

        });

        return pageResult;

    }

    public OrderVO getById(Integer id,Integer userId){
        OrderVO orderVO = orderClient.getById(id);

        if (Objects.isNull(orderVO)){
            return null;
        }
        //加上司机收款信息，电话，车辆信息
        orderVO.setDriverVO(driverClient.getById(orderVO.getDriverId(),userId));
        orderVO.setTruckVO(truckClient.getById((orderVO.getTruckId()),orderVO.getCompanyId(),userId));
        //确认是否为常用车辆
        List<CompanyDriverVO> companyDriverVOS = companyClient.getByDriverIdAndCompanyId(orderVO.getDriverId(), orderVO.getCompanyId());
        if (CollectionUtils.isNotEmpty(companyDriverVOS)){
            CompanyDriverVO companyDriverVO = companyDriverVOS.get(Global.ZERO);
            if (Objects.nonNull(companyDriverVO.getTruckId()) && Objects.nonNull(orderVO.getTruckId()) && orderVO.getTruckId().equals(companyDriverVO.getTruckId()) ){
                orderVO.setUsual(OrderUsualEnum.YES.name());
            }

        }
        return orderVO;

    }


}
